<template>
  <!-- 搜索框 -->
  <div class="searchBox">
    <el-row :gutter="20" class="search">
      <el-col :span="16">
        学科名称：
        <el-input style="width: 25%" v-model="search"></el-input>
        <!-- 按钮 -->
        <el-button style="margin-left: 10px" @click="eliminate">清除</el-button>
        <el-button type="primary" @click="searchitem">搜索</el-button>
      </el-col>
      <el-col :span="8" class="bg-purple"
        ><el-button type="success" icon="el-icon-edit" @click="add"
          >新增按钮</el-button
        ></el-col
      >
    </el-row>

    <!-- 数据 -->

    <span slot="before" class="el-icon-info data">数据一共{{ total }}条</span>

    <!-- 表单 -->
    <el-table :data="list" style="width: 100%">
      <el-table-column label="序号" width="120" type="index"> </el-table-column>
      <el-table-column prop="subjectName" label="学科名称" width="120">
      </el-table-column>
      <el-table-column prop="username" label="创建者"> </el-table-column>
      <el-table-column
        prop="addDate"
        label="创建日期"
        width="120"
      ></el-table-column>
      <el-table-column prop="isFrontDisplay" label="前台是否显示" width="120">
      </el-table-column>
      <el-table-column prop="twoLevelDirectory" label="二级目录" width="120">
      </el-table-column>
      <el-table-column prop="tags" label="标签" width="120"> </el-table-column>
      <el-table-column prop="totals" label="题目数量" width="120">
      </el-table-column>
      <el-table-column prop="name" label="操作" width="230">
        <template :gutter="20" slot-scope="{ row }" style="font-size: 12px">
          <router-link :to="{ path: 'directorys', query: { ...row } }"
            ><el-button :span="6" type="text">学科分类</el-button></router-link
          >
          <router-link :to="{ path: 'tags1', query: { ...row } }">
            <el-button :span="6" type="text">学科标签</el-button></router-link
          >
          <el-button :span="6" type="text" @click="edit(row)">修改</el-button>
          <el-button :span="6" type="text" @click="rowsubjects(row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <div class="block">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="100"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
    <!-- 新增学员 -->
    <el-dialog
      :model="formData"
      :title="name"
      :visible="showDialog"
      @close="btnCancel"
    >
      <el-form :rules="rules" ref="formData" :model="formData">
        <el-form-item label="学科名称:" prop="subjectName">
          <el-input
            style="width: 50%"
            v-model="formData.subjectName"
          ></el-input>
        </el-form-item>
        <!-- 开关滑块 -->

        <el-switch
          inactive-text="是否显示"
          v-model="formData.isFrontDisplay"
          :active-value="1"
          :inactive-value="0"
        >
        </el-switch>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="btnCancel">取 消</el-button>
        <el-button type="primary" @click="btnOK">确 定</el-button>
      </div>
    </el-dialog>
    <!--  -->
  </div>
</template>

<script>
import {
  subjects,
  rowsubjects,
  subjectsadd,
  revasubjects,
  update
} from '@/api/hmmm/subjects1.js'
import { parseTime } from '../../filters'

// 导入组件

export default {
  name: 'subJects',
  data () {
    return {
      name: '新增学科',
      // 弹层校验规则
      rules: {
        subjectName: [
          { required: true, message: '学科名称不能为空', trigger: 'blur' }
        ]
      },
      // 滑块按钮
      value: true,
      formData: {
        subjectName: '', // 学科名称
        isFrontDisplay: 1 // 开启
      },
      // 显示框
      showDialog: false,
      // 搜索框内容
      search: '',
      // 接受后台数据
      list: [],
      // 页码
      page: {
        page: 1,
        pagesize: 10
      },
      // 剩余一页时隐藏分页
      // 总数
      total: 0
    }
  },
  mounted () {},
  components: {},
  props: {},
  computed: {},
  watch: {},
  created () {
    this.subjects()
  },
  methods: {
    // 新增
    async btnOK () {
      try {
        await this.$refs.formData.validate()
        // 只有校验通过的情况下 才会执行await的下方内容
        // roleForm这个对象有id就是编辑 没有id就是新增
        if (this.formData.id) {
          await update(this.formData)
        } else {
          // 新增业务
          await subjectsadd(this.formData)
        }
        // 重新拉取数据
        this.subjects()
        this.$message.success('操作成功')
        this.showDialog = false // 关闭弹层  =>  触发el-dialog的事件close事件
      } catch (error) {
        console.log(error)
      }
    },
    // 注册取消，关闭弹窗
    btnCancel () {
      // 关闭弹层
      this.showDialog = false
      // 清空表单数据
      this.formData = {
        subjectName: '',
        isFrontDisplay: 0
      }
      // 重置表单校验
      this.$refs.formData.resetFields()
    },
    // 编辑
    async edit (obj) {
      this.showDialog = true
      // 获取角色详情，数据回显
      const { data } = await revasubjects(obj)
      console.log(data)
      this.formData = data
      console.log(this.formData)
      if (this.formData.id) {
        this.name = '修改学科'
      }
    },

    // 清除内容
    eliminate () {
      this.search = ''
    },
    async searchitem () {
      if (this.search === '') {
        this.subjects()
      }
      const arr = []
      this.list.forEach((item) => {
        // console.log(item.subjectName.includes(this.search))
        if (item.subjectName.includes(this.search)) {
          arr.push(item)
        }
      })
      console.log(arr)
      // 把数组赋值给渲染页面的数组
      this.list = arr
      // 修改传入组件的条数
      this.total = arr.length
    },
    // 获取用户信息
    async subjects () {
      const { data } = await subjects(this.page) // console.log(data)
      this.list = data.items
      this.list.map((item) => {
        item.isFrontDisplay = item.isFrontDisplay === 1 ? '是' : '否'
        // 格式化时间
        item.addDate = parseTime(item.addDate)
      })
      // console.log(item)
      this.total = data.counts
    },
    // 每页显示的数据
    handleSizeChange (val) {
      this.page.pageSize = val
      if (this.page.page === 1) {
        this.subjects(this.page)
      }
    },
    // 进入某一页
    handleCurrentChange (val) {
      this.page.page = val
      this.subjects()
    },
    // 新增
    add () {
      this.showDialog = true
    },
    // 删除
    async rowsubjects (id) {
      await this.$confirm('您确定删除该员工吗')
      await rowsubjects(id)
      this.subjects()
      this.$message.success('删除员工成功')
    }
  }
}
</script>

  <style scoped>
.block {
  display: flex;
  justify-content: flex-end;
}
.data {
  background-color: #f4f4f5;
  margin: auto;
  width: 100%;
  display: inline-block;
  padding-left: 22px;
  margin: 10px;
  height: 44px;
  display: flex;
  align-items: center;
}
.search {
  padding-left: 40px;
  height: 86px;
  background-color: #fff;
  display: flex;
  align-items: center;
  width: 99%;
  margin: auto;
}
.searchBox {
  margin: 18px 10px 0 10px;
  background-color: #fff;
}
.bg-purple {
  display: flex;
  justify-content: flex-end;
}
</style>